﻿$(function () {
    $("img").attr("onerror", "this.src='/Content/Images/lose.png'");

    var gds = Tools.getShoppingCar();
    var sjson = JSON.stringify(Enumerable.from(gds).select(function (s) { return s.rushID; }).toArray());

    if (gds.length > 0) {
        //请求购物车数据
        $.get({
            url: "/api/good",
            dataType: "json",
            async: false,
            data: { "jdis": sjson },
            success: function (data) {
                data = JSON.parse(data);
                //本地购物车数据
                for (var i = 0; i < gds.length; i++) {
                    var g = gds[i];
                    //将匹配到的本地购物车记录添加详细信息
                    for (var j = 0; j < data.length; j++) {
                        if (g.rushID == data[j].rushID) {
                            g.title = data[j].title;
                            g.src = data[j].src;
                            g.rem = data[j].rem;
                        }
                    }
                    //添加到购物车列表
                    Tools.addCarItem(g.rushID, g.title, g.src, g.byCount, g.rem);
                }
            }
        });
    }

    var scrollFunc = function (e) {
        e = e || window.event;
        var top = $(document).scrollTop();
        var car = $("#top-shopcard")[0];
        var v = 100;
        if (top < 100) {
            if ($(".m-nav").css("position") == "initial") return;
            $(".m-nav").css("position", "initial");
            car.remove();
            $(".m-header > .rig").append(car);
        }
        if (top >= v) {
            if ($(".m-nav").css("position") == "fixed") return;
            $(".m-nav").css("position", "fixed").css("top", "0");
            car.remove();
            $(".m-nav").append(car);
        }
    }
    window.onmousewheel = document.onmousewheel = scrollFunc;
    $("#goSettle").click(function () {
        var count = $("#top-shopcard .bylist .list .item").length;
        if (count == 0) {
            layer.msg("没有可结算的商品!", {
                icon: 5,
                time: 2000,
                anim: 6,
            });
            return;
        }
        var bys = encodeURI(JSON.stringify(Tools.getShoppingCar()));
        window.location = "/Pay/PayIndex?bys=" + bys;
    })

    Tools.bindGdItemBtnEvents();

    if ($(".rollist .item").length > 0) {
        //滚动列表
        anime({
            targets: $(".rollist .item:first")[0],
            marginTop: -100,
            duration: 3000,
            easing: "linear",
            loop: true,
            loopComplete: function (anme) {
                var dom = $(anme.animatables[0].target);
                dom.remove().css("margin-top", "0");
                $(".rollist").append(dom);
                anme.animatables[0].target = $(".rollist .item:first")[0];
            }
        })
    }

    $("#btn-search").on("click", function () {
        var que = $("#input-search").val().trim();
        if (!que || que.length == 0) return;
        if (que.length == 1) {
            layer.msg("关键字长度不能小于1", {icon:2});
            return;
        }
        var herf = encodeURI("/home/search?q=" + que);
        window.location = herf;
    });
    $("#input-search").on("keydown", function (e) {
        if (e.keyCode == 13) {
            $("#btn-search").click();
        }
    })

    //清除购物车
    $("#clearCar").click(function () {
        var len = $("#top-shopcard .bylist .list .item").length;
        if (len == 0) {
            layer.msg("购物车还没有任何商品");
            return;
        }
        layer.confirm('您确定要清空购物车?', {
            btn: ['确认', '取消'], //按钮
            title: "提示"
        }, function () {
            Tools.delShoopingCar();
            layer.msg('购物车已清空!', {
                icon: 1,
                time: 2000,
            });
        });
    });

    $(".path-navs a").each(function (i) {
        var count = $(".path-navs a").length;
        if (i < count - 1) {
            $(this).after("<var>&nbsp;&gt;&nbsp;</var>");
        }
    })

    $(".favorite").on("click", function () {
        if (!window.MyUser) {
            layer.confirm("登陆后即可收藏,是否立即前往登陆?", { title: "提示" }, function (index) {
                window.location = "/userlogin/login";
            });
            return;
        }

        let isstar = true;
        let gid = $(this).attr("data-gid");
        if ($(this).children(".layui-icon-star").length > 0) {
            $(this).addClass("animation collected");
            $(this).children(".layui-icon-star").removeClass("layui-icon-star").addClass("layui-icon-star-fill").css("font-size", "1.4rem");
            isstar = true;
        }
        else if ($(this).children(".layui-icon-star-fill").length > 0) {
            $(this).removeClass("animation collected");
            $(this).children(".layui-icon-star-fill").removeClass("layui-icon-star-fill").addClass("layui-icon-star").css("font-size", "1.3rem");;
            isstar = false;
        }
        $.post({
            url: isstar ? "/userinfo/addcollect" : "/userinfo/cancelcollect",
            data: { gid: gid },
            dataType: "json",
            success: function (res) {
                if (res.Isucc) {
                    layer.msg(isstar ? "收藏成功!" : "已取消收藏");
                }
                else {
                    layer.msg(isstar ? "收藏失败!" : "取消收藏失败");
                }
            }
        })
    })

    //获取用户的收藏列表
    $.get({
        url: "/userinfo/usercollects",
        dataType: "json",
        success: function (data) {
            showStar(data);
        }
    })

    //将所有已收藏的项点亮
    function showStar(list) {
        $(".favorite").each(function (idx, elm) {
            let gid = parseInt($(elm).attr("data-gid"));
            if (list.has(gid)) {
                elm.firstElementChild.className = "layui-icon layui-icon-star-fill";
            }
        })
    }
})